package com.ny.controller;

import com.ny.entity.Device;
import com.ny.entity.Result;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/devices")
    public Result findAllDevices() {
        List<Device> devices = deviceService.findAllDevices();
        if (devices != null && !devices.isEmpty()) {
            return Result.success(devices);
        } else {
            return Result.error("未找到设备信息");
        }
    }

    // 添加根据设备名称查询设备信息的接口
    //请求方法: GET
    //请求URL: http://127.0.0.1:8081/devices/search?deviceName=传感器
    //参数: deviceName - 设备名称（支持模糊查询）
    //这样就可以根据设备名称模糊查询设备信息了。
    @GetMapping("/devices/search")
    public Result findDevicesByName(@RequestParam String deviceName) {
        List<Device> devices = deviceService.findDevicesByName(deviceName);
        if (devices != null && !devices.isEmpty()) {
            return Result.success(devices);
        } else {
            return Result.error("未找到设备信息");
        }
    }
}
